package com.dongdong.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.dongdong.em.Result;
import com.dongdong.em.ResultEnum;
import com.dongdong.model.TbSeller;
import com.dongdong.page.PageParams;
import com.dongdong.service.SellerService;
import com.dongdong.util.JWTUtil;
import com.dongdong.vo.SellerVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("seller")
@RefreshScope    //开启热更新
public class SellerController {

    @Autowired
    private SellerService sellerService;

    @Autowired
    private JWTUtil jwtUtil;

    @GetMapping("getSellerByName")
    public TbSeller getSellerByName(@RequestParam String username){
        if (Objects.equals("qiandu",username)){
            try {
                Thread.sleep(50);//超过50毫秒进入睡眠
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else if (Objects.equals("yijia",username)){//访问id为2的时候  会走到这里  如果快速刷新5下  就会触发5个异常
            throw new RuntimeException("手动创建的异常");
        }
        TbSeller seller = sellerService.getSellerByName(username);
        return seller;
    }

    //通过token进行解析  拿到用户信息  回显到修改资料表中    注意：shiro内部是一个session不能跨服务共享
    @GetMapping("getSellerById")
    public Result getSellerById(@RequestHeader String Autherizetion){
        //逻辑：通过获取当前登录对象id  查询出来商家表回显到页面
        //拿到token进行解析直接使用token中的信息 进行查询
       if (StrUtil.isEmpty(Autherizetion)){
           return Result.ZHRESPONSE(ResultEnum.TOKEN_LOSE_EFFICACY);
       }
        Claims claims = null;
        try {
            claims = jwtUtil.parseJwt(Autherizetion);
        } catch (Exception e) {
            return Result.ZHRESPONSE(ResultEnum.TOKEN_LOSE_EFFICACY);
        }
        String subject = claims.getSubject();
        SellerVO sellerVO = JSON.parseObject(subject, SellerVO.class);

        //根据id进行查询商家表
        TbSeller tbSeller = sellerService.getSellerById(sellerVO.getSellerId());
        return Result.OK().setData(tbSeller);
    }

    @GetMapping("getId")
    public Result getId(@RequestParam String sellerId){
        SellerVO vo = sellerService.getId(sellerId);
        return Result.OK().setData(vo);
    }

    //注册
    @PostMapping("register")
    public Result register(@RequestBody TbSeller seller){
        int i = sellerService.register(seller);
        if (i <= 0){
            return Result.ZHRESPONSE(ResultEnum.ERROR);
        }else if (i == 999){
            return Result.ZHRESPONSE(ResultEnum.REGISTER_USER_EXIST);//用户已经存在
        }
        return Result.ZHRESPONSE(ResultEnum.REGISTER_SUCCESS_DATA);
    }



    @PutMapping("updateSeller")
    public Result updateSeller(@RequestBody TbSeller seller){
        int i = sellerService.updateSeller(seller);
        if (i <= 0){
            return Result.ERROR();
        }
        return Result.OK().setData(seller);
    }


    //查询全部
    @PostMapping("getSllerList")
    public Result getSellerList(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
        PageParams<SellerVO> sellerVOList = sellerService.getSellerList(pageSize, pageNum);
        return Result.OK().setData(sellerVOList);
    }

    //模糊查询公司名称和店铺名称
    @PostMapping("getNameAndLinkeName")
    public Result getNameAndLinkeName(@RequestBody SellerVO sellerVO){
        List<SellerVO> sellerVOList = sellerService.getNameAndLinkeName(sellerVO);
        return Result.OK().setData(sellerVOList);
    }

    @GetMapping("getStaus")
    public Result getStaus(@RequestParam String status){
        List<SellerVO> sellerVOList=sellerService.getStatus(status);
        return Result.OK().setData(sellerVOList);
    }

    //改变审核状态
    @PutMapping("SetAudit")
    public Result SetAudit(@RequestParam String sellerId,@RequestParam Integer status){
        int i = sellerService.SetAudit(sellerId,status);
        if (i <= 0){
            return Result.ERROR();
        }
        return Result.OK();
    }
}
